﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    /// <summary>
    /// The Product class is used to manage various Product that you have developed.
    /// </summary>
    public class Product : EntityBase
    {
        /// <summary>
        /// Gets or sets the Product name/description.
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets or sets if the Product has been discontinued.
        /// </summary>
        public virtual bool Discontinued { get; set; }

        /// <summary>
        /// Gets or sets the date by which the Product information has been updated.
        /// </summary>
        public virtual DateTime Modified { get; set; }

        public virtual ProductCategory Category { get; set; }

        /// <summary>
        /// Gets a product by Product Id.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static Product GetProduct(ISession session, int productId)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Id", productId));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(Product));
            if (entities != null && entities.Count == 1)
            {
                return (Product)entities[0];
            }
            return null;
        }

        /// <summary>
        /// Gets a list of products which are active and have not been discontinued.
        /// </summary>
        /// <param name="session"></param>
        /// <returns>Active Product list.</returns>
        public static IList<Product> GetActiveProducts(ISession session)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            expressions.Add(Restrictions.Eq("Discontinued", false));
            IList<EntityBase> entities = Find(session, expressions, null, typeof(Product));
            return (from p in entities select p as Product).ToList();
        }
    }
}
